package com.schoolgard.action.device;

import java.util.List;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;

import com.schoolegard.services.HdTypeService;
import com.schoolgard.base.BaseAction;
import com.schoolgard.entitybean.GridDataModel;
import com.schoolgard.entitybean.HdType;

public class HdTypeAction extends BaseAction{

	private HdTypeService hdTypeService;
	
	private HdType hdType;
	
	public void save(){
		hdTypeService.saveOrUpdate(hdType);
		writeToResponse("1");
	}
	
	public void delete(){
		String id = request.getParameter("id");
		if(StringUtils.isEmpty(id)){
			writeToResponse("0");
			return;
		}else{
			hdTypeService.deleteType(Long.valueOf(id));
			writeToResponse("1");
		}
	}
	
	public void edit(){
		hdType = hdTypeService.queryTypeById(hdType.getId());
		writeToResponse("1");
	}
	
	public void queryList(){
		String startStr = request.getParameter("start"); //grid传过来的当前数据条数
		 String limitStr = request.getParameter("limit"); //每页显示的数据数
		 
		 int start = Integer.parseInt(startStr);
	     int limit = Integer.parseInt(limitStr);
	     if (limit == 0) {
	    	 limit = Integer.MAX_VALUE;
	     }
	     int end = start + limit;  //当前页需要显示的数据 start--> end
	     List<HdType> hdTypeList = hdTypeService.queryTypeAll();
		int total = hdTypeList.size();
		if(end > total)end = total;
		
		GridDataModel<HdType> page = new GridDataModel<HdType>();
		page.setPage(start/limit + 1);
		page.setRows(hdTypeList);
		page.setTotal(total);
		String jsonStr = JSONObject.fromObject(page).toString();
		writeToResponse(jsonStr);
	}

	public HdTypeService getHdTypeService() {
		return hdTypeService;
	}

	public void setHdTypeService(HdTypeService hdTypeService) {
		this.hdTypeService = hdTypeService;
	}

	public HdType getHdType() {
		return hdType;
	}

	public void setHdType(HdType hdType) {
		this.hdType = hdType;
	}
}
